package com.appspot.jevemanager.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Jevemanager implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	private final SkillTreeServiceAsync skillTreeService = GWT.create(SkillTreeService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final Label status = new Label();
		final Label playerNumber = new Label();
		RootPanel.get("serverStatus").add(status);
		RootPanel.get("playerNumber").add(playerNumber);
		
		skillTreeService.isServerOnline(
				new AsyncCallback<Boolean>() {
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						status.setText(SERVER_ERROR);
					}

					public void onSuccess(Boolean result) {
						if (result.booleanValue()) {
							status.setStylePrimaryName("verde");
							status.setText("Online");
						} else {
							status.setStylePrimaryName("rojo");
							status.setText("Offline");
						}
					}
				});
		skillTreeService.getOnlinePlayers(
				new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						status.setText(SERVER_ERROR);
					}

					public void onSuccess(String result) {
						playerNumber.setText(result);
					}
				});
	}
}
